package com.malbbmsomanager.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@SuppressLint("NewApi") public class DM_BBM_Inquiry_Adapters {
	// Database Name
	private static final String DATABASE_NAME = "dminvoicemanagerbbm.db";
	// Master Tables BBM
	private String ITEMS_INQUIRY = "Items_Inquiry";
	private static final int DATABASE_VERSION = 4;
	private static final String TAG = null;
	private DatabaseHelper dbHelperItemInquiry;
	private SQLiteDatabase mDb;

	public static final String KEY_IQ_ID = "_id";
	public static final String KEY_IQ_ITEMCODE = "ItemCode";
	public static final String KEY_IQ_TYPENAME = "TypeName";
	public static final String KEY_IQ_ITEMNAME = "ItemName";
	public static final String KEY_IQ_PURDESC = "PurDesc";
	public static final String KEY_IQ_BRANDNAE = "BrandName";
	public static final String KEY_IQ_PACKING = "Packing";
	public static final String KEY_IQ_FINALQTY = "FinalQty";
	public static final String KEY_IQ_RESVQTY = "ResvQTY";
	public static final String KEY_IQ_STDPRICE = "STDPrice";
	public static final String KEY_IQ_USDPRICE = "USDPrice";
	public static final String KEY_IQ_UNDIPQTY = "UnDispQTY";
	public static final String KEY_IQ_PACKSIZE = "PackSize";
	public static final String KEY_IQ_TAXCODE = "TAXCode";
	public static final String KEY_IQ_SUMMARY = "IQSummary";
	

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		private static final String TAG = null;

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
		}
	}

	public DM_BBM_Inquiry_Adapters(Context ctx) {
		this.mCtx = ctx;
	}

	public DM_BBM_Inquiry_Adapters open() throws SQLException {
		dbHelperItemInquiry = new DatabaseHelper(mCtx);
		mDb = dbHelperItemInquiry.getWritableDatabase();
		return this;
	}

	public void close() {
		if (dbHelperItemInquiry != null) {
			dbHelperItemInquiry.close();
		}
	}

	// *****CUSTOMER DETAILS*****
	public Cursor fetchAllItemsInquiry() {
		Log.v(TAG, "DJ-Filter Customers");
		Cursor mCursor = mDb.query(ITEMS_INQUIRY, new String[] { KEY_IQ_ID, KEY_IQ_ITEMCODE, KEY_IQ_TYPENAME, KEY_IQ_ITEMNAME, KEY_IQ_PURDESC, KEY_IQ_BRANDNAE, KEY_IQ_PACKING, KEY_IQ_FINALQTY, KEY_IQ_RESVQTY, KEY_IQ_STDPRICE, KEY_IQ_USDPRICE ,KEY_IQ_UNDIPQTY, KEY_IQ_PACKSIZE, KEY_IQ_TAXCODE, KEY_IQ_SUMMARY }, null, null, null, null, null);


		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}

	// *****CUSTOMER DETAILS ORDER BY*****
		public Cursor fetchAllItemsInquiryOrderBy() {
			Log.v(TAG, "DJ-Filter Customers");
			String orderByCus =  DM_BBM_Inquiry_Adapters.KEY_IQ_ITEMCODE + " ASC";
			Cursor mCursor = mDb.query(ITEMS_INQUIRY, new String[] { KEY_IQ_ID, KEY_IQ_ITEMCODE, KEY_IQ_TYPENAME, KEY_IQ_ITEMNAME, KEY_IQ_PURDESC, KEY_IQ_BRANDNAE, KEY_IQ_PACKING, KEY_IQ_FINALQTY, KEY_IQ_RESVQTY, KEY_IQ_STDPRICE, KEY_IQ_USDPRICE ,KEY_IQ_UNDIPQTY, KEY_IQ_PACKSIZE, KEY_IQ_TAXCODE, KEY_IQ_SUMMARY }, null, null, null, null, null);

			if (mCursor != null) {
				mCursor.moveToNext();
			}
			return mCursor;
		}

	public Cursor fetchAllItemsInquiryBySummary(String inputText)
			throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(ITEMS_INQUIRY, new String[] {  KEY_IQ_ID, KEY_IQ_ITEMCODE, KEY_IQ_TYPENAME, KEY_IQ_ITEMNAME, KEY_IQ_PURDESC, KEY_IQ_BRANDNAE, KEY_IQ_PACKING, KEY_IQ_FINALQTY, KEY_IQ_RESVQTY, KEY_IQ_STDPRICE, KEY_IQ_USDPRICE ,KEY_IQ_UNDIPQTY, KEY_IQ_PACKSIZE, KEY_IQ_TAXCODE, KEY_IQ_SUMMARY }, null, null, null, null, null);

		} else {
			mCursor = mDb.query(true, ITEMS_INQUIRY, new String[] {  KEY_IQ_ID, KEY_IQ_ITEMCODE, KEY_IQ_TYPENAME, KEY_IQ_ITEMNAME, KEY_IQ_PURDESC, KEY_IQ_BRANDNAE, KEY_IQ_PACKING, KEY_IQ_FINALQTY, KEY_IQ_RESVQTY, KEY_IQ_STDPRICE, KEY_IQ_USDPRICE ,KEY_IQ_UNDIPQTY, KEY_IQ_PACKSIZE, KEY_IQ_TAXCODE, KEY_IQ_SUMMARY }, KEY_IQ_SUMMARY + " like '%"
					+ inputText + "%'", null, null, null, null, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}
	
}
